package org.opens.javaskill.algorithm;

import org.junit.Test;

public class LowLevelAlgorithms {

    /**
     * 简介:
     *      该算法是用来快速交换两个数的值的.
     * 输出:
     *      a: 23, b:2999
     *      a: 2999, b:23
     */
    @Test
    public void test1() {
        int a = 23;
        int b = 2999;
        System.out.println("a: " + a + ", b:" + b);
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println("a: " + a + ", b:" + b);
    }

    /**
     * 功能:
     *      该算法的功能是计算出一个数组中和为9的两个数的下标, 并返回.
     */
    @Test
    public void test2() {
        int[] nums = {1, 25, 56, 5, 9, 14, 5, 7, 8};
        if(nums.length == 0) {
            return;
        }
        int length = nums.length;
        int[] resultTemp1 = new int[length];
        int minValue = nums[0];
        int[] res = {-1, -1};

        int numberLessThanNine = 0;
        int numberGreaterThanNine = 0;
        if(length < 2) {
            return;
        }
        for(int i = 0; i < length; i++) {
            if(nums[i] < 9) {
                numberLessThanNine++;
                if(nums[i] < minValue) {
                    minValue = nums[i];
                }
            } else {
                numberGreaterThanNine++;
            }
        }
        if(numberLessThanNine == 0) {
            return;
        }
        if(numberLessThanNine / length < 0.2f) {
            int[] resultTemp2 = new int[numberLessThanNine];
            int[] arraySubscript = new int[numberLessThanNine];
            int index = 0;
            for(int z = 0; z < length; z++) {
                if(nums[z] <= 9 - minValue) {
                    resultTemp2[index] = nums[z];
                    arraySubscript[index] = z;
                    index++;
                }
            }
            int length2 = resultTemp2.length;
            for(int q = 0; q < length2 - 1; q++) {
                for(int w = q + 1; w < length2; w++) {
                    if(resultTemp2[q] + resultTemp2[w] == 9) {
                        res[0] = arraySubscript[q];
                        res[1] = arraySubscript[w];
                        break;
                    }
                }
            }
        } else {
            for(int q1 = 0; q1 < length - 1; q1++) {
                for(int w1 = q1 + 1; w1 < length; w1++) {
                    if(nums[q1] + nums[w1] == 9) {
                        res[0] = q1;
                        res[1] = w1;
                        break;
                    }
                }
            }
        }

        for (int re : res) {
            System.out.println(re);
        }
    }

    /**
     * leetcode算法第27题
     */
    @Test
    public void test3() {
        System.out.println(removeElement(new int[] {0,1,2,0,0,0,0,2,3,0,4,2,2,2,2,3,2}, 2));
    }

    private int removeElement(int[] nums, int val) {
        int j = nums.length - 1;
        int i = 0;
        int len = 0;
        while(i < j) {
            if(nums[i] == val) {
                while(nums[j] == val) {
                    if(j <= i) {
                        break;
                    }
                    j--;
                }
                if(i < j && nums[j] != val) {
                    nums[i] = nums[i] ^ nums[j];
                    nums[j] = nums[i] ^ nums[j];
                    nums[i] = nums[i] ^ nums[j];
                }
            } else {
                i++;
            }
        }

        for(int q = 0; q < nums.length; q++) {
            if(nums[q] == val) {
                break;
            }
            len++;
        }
        return len;
    }


}
